home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / finger-1 / my_units / myprinti.uni < prev    next >
Text File  |  1992-02-24  |  7KB  |  253 lines

  1. unit MyPrinting;
  2.  
  3. { This code is part of the Finger/Fingerd source code, written in THINK Pascal 4 }
  4. { Copyright 1991-1992 Peter N Lewis }
  5. { If you use this code, you must give me credit in your about box and documentation }
  6. { This is part of my generic library of routines }
  7.  
  8. interface
  9.  
  10.     uses
  11.         Printing;
  12.  
  13.     type
  14.         PObject = object
  15.                 procedure Create;
  16.                 procedure Destroy;
  17.                 function CountPages (r: rect): integer;
  18.                 procedure DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean);
  19.                 procedure OpenPrintingStatusDialog;
  20.                 procedure DoIdle;
  21.                 procedure ClosePrintingStatusDialog;
  22.                 procedure PostPrintingErrors (oe: OSErr);
  23.             end;
  24.  
  25.     var
  26.         thePrintingRecordHandle: THPrint;
  27.  
  28.     procedure InitPrinting;
  29.     procedure FinishPrinting;
  30.     procedure PrintStuff (pob: PObject; thePrRecHdl: THPrint);
  31.     procedure DoPageSetup (pob: PObject; thePrRecHdl: THPrint);
  32.     procedure SegmentPrinting;
  33.  
  34. implementation
  35.  
  36.     uses
  37.         BaseGlobals;
  38.  
  39. {$S Printing}
  40.     procedure SegmentPrinting;
  41.     begin
  42.     end;
  43.  
  44. {$S Printing}
  45.     procedure PObject.Create;
  46.     begin
  47.     end;
  48.  
  49. {$S Printing}
  50.     procedure PObject.Destroy;
  51.     begin
  52.         dispose(self);
  53.     end;
  54.  
  55. {$S Printing}
  56.     function PObject.CountPages (r: rect): integer;
  57.     begin
  58.         CountPages := 1;
  59.     end;
  60.  
  61. {$S Printing}
  62.     procedure PObject.DoIdle;
  63.     begin
  64.     end;
  65.  
  66. {$S Printing}
  67.     procedure PObject.DrawPage (r: rect; gp: GrafPtr; pg: integer; first, last: boolean);
  68.     begin
  69.         SetPort(gp);
  70.         with r do
  71.             MoveTo((left + right) div 2 - 20, (top + bottom) div 2);
  72.         DrawString('Not Yet Implemented');
  73.     end;
  74.  
  75. {$S Printing}
  76.     procedure PObject.PostPrintingErrors (oe: OSErr);
  77.         var
  78.             s: str255;
  79.             a: integer;
  80.     begin
  81.         NumToString(oe, s);
  82.         ParamText('Print Error = ', s, '', '');
  83.         a := Alert(fail_alert_id, nil);
  84.     end;
  85.  
  86. {$S Printing}
  87.     procedure PObject.OpenPrintingStatusDialog;
  88.     begin
  89.         SetCursor(GetCursor(watchCursor)^^);
  90.     end;
  91.  
  92. {$S Printing}
  93.     procedure PObject.ClosePrintingStatusDialog;
  94.     begin
  95.         SetCursor(arrow);
  96.     end;
  97.  
  98.     var
  99.         gpob: PObject;
  100.  
  101. {$S}
  102.     procedure DoIdle;
  103.     begin
  104.         gpob.DoIdle;
  105.     end;
  106.  
  107. {$S Init}
  108.     procedure InitPrinting;
  109.     begin
  110.         thePrintingRecordHandle := THPrint(NewHandle(SIZEOF(TPrint)));
  111.         PrOpen;
  112.         if PrError = noErr then begin
  113.             PrintDefault(thePrintingRecordHandle);
  114.             PrClose;
  115.         end;
  116.     end;
  117.  
  118. {$S Term}
  119.     procedure FinishPrinting;
  120.     begin
  121.         DisposHandle(handle(thePrintingRecordHandle));
  122.     end;
  123.  
  124. {$S}
  125.     procedure DoPageSetup (pob: PObject; thePrRecHdl: THPrint);
  126.         var
  127.             dummy: boolean;
  128.     begin
  129.         PrOpen;
  130.         if PrError = noErr then
  131.             dummy := PrStlDialog(thePrRecHdl)
  132.         else
  133.             pob.PostPrintingErrors(PrError);
  134.         PrClose;
  135.     end;
  136.  
  137. {*------ PrintStuff ---------------------------------------------------------*}
  138. {** **   PrintStuff will call all of the necessary Print Manager calls to print }
  139. {**   a document.  It checks PrError() after each Print Manager call.  If an }
  140. { **   error is found, all of the Print Manager open calls (i.e., PrOpen, }
  141. { **   PrOpenDoc...) will have a corresponding close call before the error }
  142. { **   is posted to the user.  You want to use this approach to make sure the }
  143. { **   Print Manager closes properly and all temporary memory is released. }
  144. {$S Printing}
  145.     procedure PrintStuff (pob: PObject; thePrRecHdl: THPrint);
  146.  
  147.         var
  148.             copies, firstPage, lastPage, loop, numberOfCopies, pageNumber, printmgrsResFile, realNumberOfPagesInDoc: Integer;
  149.             PrintError: LongInt;
  150.             oldPort: GrafPtr;
  151.             thePrPort: TPPrPort;
  152.             theStatus: TPrStatus;
  153.  
  154.     begin
  155.         GetPort(oldPort);
  156.         gpob := pob;
  157.  
  158.         PrOpen;
  159.         if PrError = noErr then begin
  160.              { Save the current resource file (i.e. the printer driver's) so the driver will not lose its }
  161.             { resources upon return from the pIdleProc.}
  162.             printmgrsResFile := CurResFile;
  163.  
  164.             realNumberOfPagesinDoc := pob.CountPages(thePrRecHdl^^.prInfo.rPage);
  165.  
  166.             if PrJobDialog(thePrRecHdl) then begin
  167.  {                          Get the number of copies of the document that}
  168.  {                          the user wants printed from iCopies of the TPrJob}
  169.   {                         record (IM II-151).}
  170.  
  171.                 numberOfCopies := thePrRecHdl^^.prJob.iCopies;
  172.  
  173.   {                           Get the first and last pages of the document that}
  174.   {                          were requested to be printed by the user from}
  175.    {                         iFstPage and iLastPage from the TPrJob record}
  176.    {                         (IM II-151).}
  177.  
  178.                 firstPage := thePrRecHdl^^.prJob.iFstPage;
  179.                 lastPage := thePrRecHdl^^.prJob.iLstPage;
  180.  
  181. {                             Print "all" pages in the print loop}
  182.  
  183.                 thePrRecHdl^^.prJob.iFstPage := 1;
  184.                 thePrRecHdl^^.prJob.iLstPage := 9999;
  185.                 if (realNumberOfPagesinDoc < lastPage) then
  186.                     lastPage := realNumberOfPagesinDoc;
  187.  
  188.   {                           Print the number of copies of the document}
  189.  {                           requested by the user from the Print Job Dialog.}
  190.                 pob.OpenPrintingStatusDialog;
  191.  
  192.                 for copies := 1 to numberOfCopies do begin
  193.  {                               Install and call your "Print Status Dialog".}
  194.                     thePrRecHdl^^.prJob.pIdleProc := @DoIdle;
  195.  
  196.                     UseResFile(printmgrsResFile);
  197.  
  198.                     thePrPort := PrOpenDoc(thePrRecHdl, nil, nil);
  199.  
  200.                     if (PrError = noErr) then begin
  201.                             {  Print the range of pages of the document requested by the user from the Print Job Dialog.}
  202.  
  203.                         pageNumber := firstPage;
  204.                         while ((pageNumber <= lastPage) and (PrError = noErr)) do begin
  205.  
  206.                             PrOpenPage(thePrPort, nil);
  207.  
  208.                             if (PrError = noErr) then begin
  209.                                 { rPage (IM II-150) is the printable area for the currently selected printer. By passing the current}
  210.                               { enables your app to use the same routine to draw to the screen and the printer's GrafPort.}
  211.  
  212.                                 pob.DrawPage(thePrRecHdl^^.prInfo.rPage, GrafPtr(thePrPort), pageNumber, firstPage = pageNumber, lastPage = pageNumber);
  213.                             end;
  214.                             PrClosePage(thePrPort);
  215.                             pageNumber := pageNumber + 1;
  216.                         end;  {**  End pagenumber loop  **}
  217.                     end;
  218.                     PrCloseDoc(thePrPort);
  219.                 end;  {**  End copies loop  **}
  220.  
  221.                 pob.ClosePrintingStatusDialog;
  222.   {                            The printing job is being canceled by the request}
  223.   {                            of the user from the Print Style Dialog or the}
  224.   {                            Print Job Dialog PrError will be set to iPrAbort}
  225.    {                           to tell the Print Manager to abort the current}
  226.    {                           printing job.}
  227.             end
  228.             else
  229.                 PrSetError(iPrAbort); {** Cancel from the job dialog **}
  230.         end;
  231.         if (thePrRecHdl^^.prJob.bJDocLoop = bSpoolLoop) and (PrError = noErr) then
  232.             PrPicFile(thePrRecHdl, nil, nil, nil, theStatus);
  233.  
  234.   {        Grab the printing error before you close}
  235.  {        the Print Manager and the error disappears.}
  236.  
  237.         PrintError := PrError;
  238.  
  239.         PrClose;
  240.  
  241.   {       You do not want to report any printing errors until you have fallen}
  242.  {       through the printing loop. This will make sure that ALL of the Print}
  243.   {      Manager's open calls have their corresponding close calls, thereby}
  244.   {      enabling the Print Manager to close properly and that all temporary}
  245.  {       memory allocations are released.}
  246.  
  247.         if (PrintError <> noErr) and (PrintError <> iPrAbort) then
  248.             pob.PostPrintingErrors(PrintError);
  249.  
  250.         SetPort(oldPort);
  251.     end;  {**  PrintStuff  **}
  252.  
  253. end.